const db = require('../db/index.js')
const util = require('util')
const query = util.promisify(db.query).bind(db)

exports.getRank = (req, res) => {
    const query1 = `select distinct city,nowCount from covid19_daily_city where DATE_FORMAT(DATETIME,'%Y-%m-%d')=? order by nowCount desc limit 0,10`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "city": results[i].city,
                        "count": results[i].nowCount,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.cityrank = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getProportion = (req, res) => {
    const query1 = `SELECT * FROM covid19_daily_china WHERE DATE_FORMAT(DATETIME,"%Y-%m-%d")=?`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "now": results[i].nowCount,
                        "new": results[i].newCount,
                        "dead": results[i].deadCount,
                        "diagnosed": results[i].diagnosedCount,
                        "cure": results[i].cureCount
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.proportion = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getMapData = (req, res) => {
    const query1 = "select distinct city,nowCount from covid19_daily_city where DATE_FORMAT(DATETIME,'%Y-%m-%d')=? order by nowCount desc"
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "city": results[i].city,
                        "count": results[i].nowCount,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.mapdata = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getvaccworld = (req, res) => {
    const query1 = `SELECT countryName,inoculateCount,inoculateCount100 FROM covid19_vaccine_world order by inoculateCount desc limit 0,10`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "country": results[i].countryName,
                        "count": results[i].inoculateCount,
                        "count100": results[i].inoculateCount100,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.vaccworld = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getcovline = (req, res) => {
    const query1 = `SELECT countryName,newCount,diagnosedCount,deadCount,cureCount,STR_TO_DATE(DATETIME,'%Y-%m-%d') AS 'date' FROM covid19_daily_china`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "countryName": results[i].countryName,
                        "new": results[i].newCount,
                        "now": results[i].nowCount,
                        "diagnosed": results[i].diagnosedCount,
                        "dead": results[i].deadCount,
                        "cure": results[i].cureCount,
                        "date": results[i].date,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.covline = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getnews = (req, res) => {
    const query1 = `select id,STR_TO_DATE(pubDate,'%Y-%m-%d') as pubDate,title,summary,infoSource,sourceUrl from covid19_news  ORDER BY pubDate DESC  limit 0,500`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        pubDate: results[i].pubDate,
                        title: results[i].title,
                        summary: results[i].summary,
                        infoSource: results[i].infoSource,
                        sourceUrl: results[i].sourceUrl,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.news = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getmortalityratechina = (req, res) => {
    const query1 = `select deadCount/diagnosedCount as dead from covid19_daily_china WHERE DATE_FORMAT(DATETIME,"%Y-%m-%d")=?`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        dead: results[i].dead,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.mortalityratechina = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getvaccratechina = (req, res) => {
    const query1 = `SELECT inoculateCount100 FROM covid19_vaccine_china WHERE DATE_FORMAT(DATETIME,"%Y-%m-%d")=?`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        vaccrate: results[i].inoculateCount100,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.vaccrate = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getCityDataByDay = (req, res) => {
    const query1 = `select distinct city,nowCount,newCount,newdead,deadCount from covid19_daily_city where DATE_FORMAT(DATETIME,'%Y-%m-%d')=? order by nowCount desc`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {

                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "city": results[i].city,
                        "nowCount": results[i].nowCount,
                        "newCount": results[i].newCount,
                        "newdead": results[i].newdead,
                        "deadCount": results[i].deadCount
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.citydata = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getUSAline = (req, res) => {
    const query1 = `SELECT countryName,newCount,nowCount,diagnosedCount,deadCount,cureCount,STR_TO_DATE(DATETIME,'%Y-%m-%d') AS 'date' FROM covid19_daily_usa`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "countryName": results[i].countryName,
                        "new": results[i].newCount,
                        "now": results[i].nowCount,
                        "diagnosed": results[i].diagnosedCount,
                        "dead": results[i].deadCount,
                        "cure": results[i].cureCount,
                        "date": results[i].date,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.usacovline = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getWorldMapData = (req, res) => {
    const query1 = "select * from covid19_world order by diagnosedCount desc"
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "country": results[i].countryName,
                        "count": results[i].diagnosedCount,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.mapdata = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getWorldCountData = (req, res) => {
    const query1 = "SELECT SUM(diagnosedCount) AS diag,SUM(cureCount) AS cure,SUM(deadCount) AS dead FROM covid19_world"
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "diag": results[i].diag,
                        "cure": results[i].cure,
                        "dead": results[i].dead,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.countworlddata = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getCountryRank = (req, res) => {
    const query1 = `SELECT * FROM covid19_world ORDER BY diagnosedCount DESC`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "country": results[i].countryName,
                        "count": results[i].diagnosedCount,
                        "cureCount": results[i].cureCount,
                        "deadCount": results[i].deadCount,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.countryrank = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getVaccTable = (req, res) => {
    const query1 = `select * from covid19_vaccine_world`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        countryName: results[i].countryName,
                        dateTime: results[i].dateTime,
                        vaccineType: results[i].vaccineType,
                        inoculateCount: results[i].inoculateCount,
                        inoculateCount100: results[i].inoculateCount100,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.vacctable = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getvaccworld100 = (req, res) => {
    const query1 = `SELECT countryName,inoculateCount,inoculateCount100 FROM covid19_vaccine_world order by inoculateCount100 desc limit 0,10`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "country": results[i].countryName,
                        "count": results[i].inoculateCount,
                        "count100": results[i].inoculateCount100,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.vaccworld = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getvaccrateworld = (req, res) => {
    const query1 = `SELECT SUM(inoculateCount)/7000000000/3 as inorate FROM covid19_vaccine_world`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, req.body.date, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        inorate: results[i].inorate,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.vaccrateworld = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getVaccWorldMapData = (req, res) => {
    const query1 = "select * from covid19_vaccine_world order by inoculateCount100 desc"
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "country": results[i].countryName,
                        "inoculateCount": results[i].inoculateCount,
                        "inoculateCount100": results[i].inoculateCount100,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.mapdata = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}

exports.getJPline = (req, res) => {
    const query1 = `SELECT countryName,newCount,diagnosedCount,deadCount,cureCount,STR_TO_DATE(DATETIME,'%Y-%m-%d') AS 'date' FROM covid19_daily_japan`
    var obj = {
        data: {},
        meta: {
            status: '',
            msg: ''
        }
    }
    db.query(query1, (err, results) => {
        if (err) {
            obj.meta.status = 500;
            obj.meta.msg = err;
            return res.send(obj);
        } else {
            if (results.length >= 1) {
                for (var i = 0; i < results.length; i++) {
                    results[i] = {
                        "countryName": results[i].countryName,
                        "new": results[i].newCount,
                        "diagnosed": results[i].diagnosedCount,
                        "dead": results[i].deadCount,
                        "cure": results[i].cureCount,
                        "date": results[i].date,
                    }
                }
                obj.meta.msg = '查询成功';
                obj.meta.status = 200;
                obj.data.jpcovline = results;
                return res.send(obj)
            } else {
                obj.meta.status = 500;
                obj.meta.msg = '查询到了数据，但数据出错';
                return res.send(obj);
            }
        }
    })
}